﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Input;
using FFImageLoading.Forms;
using mb;
using Plugin.InputKit.Shared.Controls;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static libmp.DataStore;

namespace Shared
{
    public class SelectOperatePage : BasePage
    {
        Button btnConfirm;
        public SelectOperatePage(Action onCancel, Action onConfirm, Action<bool> onSelectAll)
        {
            if (LoadXamlFromRes("select_operate"))
            {
                btnConfirm = this.FindByName("btnConfirm") as Button;
                btnConfirm.Command = new Command(onConfirm);

                (FindByName("btnCancel") as Button).Command = new Command(onCancel);
                (FindByName("ckbSelectAll") as CheckBox).CheckChangedCommand = new Command(
                    (o) => { onSelectAll((o as CheckBox).IsChecked); }
                );
            }
        }

        public int Count
        {
            set
            {
                btnConfirm.Text = $"删除({value})";
            }
        }

    }

    public class OfflineItemPage : BasePage
    {

        Label labName;
        Label labTip;
        CachedImage image;
        BoxView eventBox;
        VideoItem videoItem;
        FFImageLoading.Forms.CachedImage checkImage;

        public OfflineItemPage(Action onPress, Action onLongPress, Action<bool> onCheckChanged = null)
        {

            if (this.LoadXamlFromRes("offline_item"))
            {
                this.labName = this.FindByName("name") as Label;
                this.labTip = this.FindByName("tip") as Label;
                this.image = this.FindByName("image") as CachedImage;
                this.eventBox = this.FindByName("eventBox") as BoxView;
                this.checkImage = this.FindByName("checkImage") as FFImageLoading.Forms.CachedImage;
                checkImage.Source = ImageSource.FromFile($"{App.HotStorageRoot}/res/user/check.png");

                // SetCheckImageColor("#ffffff");
                (checkImage.Parent as Layout).IsVisible = false;
            }

            var eo = this.eventBox;
            eo.Effects.Add(new PressedEffect());
            if (onPress != null) PressedEffect.SetCommand(eo, new Command(onPress));
            if (onLongPress != null) PressedEffect.SetLongPressCommand(eo, new Command(onLongPress));
        }

        void SetCheckImageColor(string c)
        {
            var tt = new FFImageLoading.Transformations.TintTransformation();
            checkImage.Transformations.Clear();
            tt.HexColor = c;
            tt.EnableSolidColor = true;
            checkImage.Transformations.Add(tt);
            checkImage.Source = ImageSource.FromFile($"{App.HotStorageRoot}/res/user/check.png");
        }

        public bool ShowCheckLayout
        {
            set
            {
                (checkImage.Parent as Layout).IsVisible = value;
            }
        }
        public bool Selected
        {
            set
            {
                // logger.Log("set selected", value, videoItem.vid);
                SetCheckImageColor(value? "#fb7299": "#ffffff");
            }
        }
        public void Update(VideoItem vi, (int vid, long total, long now) oi)
        {
            videoItem = vi;
            image.Source = vi.vpic;
            labName.Text = vi.vname;

            var per = (100.0f * oi.now / oi.total).ToString("0.0");
            var siz = (oi.total / 1024.0f / 1024).ToString("0.0");
            labTip.Text = $"已下载 {per}% 总:{siz}MB";

        }

    }
}